package cn.edu.zhku.service.impl;

import cn.edu.zhku.dto.UserDto;
import cn.edu.zhku.pojo.form.*;
import cn.edu.zhku.mapper.form.*;
import cn.edu.zhku.service.FormService;
import cn.edu.zhku.util.PojoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class FormServiceImpl implements FormService {

    @Autowired
    private FormMapper formMapper;
    @Autowired
    private PhenotypeMapper phenotypeMapper;
    @Autowired
    private PlantTraitsMapper plantTraitsMapper;
    @Autowired
    private BaclTraitsMapper baclTraitsMapper;
    @Autowired
    private MsclTraitsMapper msclTraitsMapper;
    @Autowired
    private SppTraitsMapper sppTraitsMapper;
    @Autowired
    private MainStemMapper mainStemMapper;
    @Autowired
    private FpolBranchesMapper fpolBranchesMapper;
    @Autowired
    private BranchMapper branchMapper;
    @Autowired
    private PodsMapper podsMapper;
    @Autowired
    private PetioleMapper petioleMapper;
    @Autowired
    private LeafletMapper leafletMapper;

    @Override
    @Transactional
    public void addOne(Form form, Phenotype phenotype, PlantTraits plantTraits,
                       BaclTraits baclTraits, MsclTraits msclTraits, SppTraits sppTraits,
                       MainStem mainStem, FpolBranches fpolBranches, Branch branch,
                       Petiole baclPetiole, Leaflet baclLeaflet, Petiole msclPetiole, Leaflet msclLeaflet,
                       Pods pods) {
        this.mainStemMapper.insertOne(mainStem);

        this.fpolBranchesMapper.insertOne(fpolBranches);

        this.branchMapper.insertOne(branch);

        plantTraits.setMainStem(mainStem);
        plantTraits.setFpolBranches(fpolBranches);
        plantTraits.setBranch(branch);
        this.plantTraitsMapper.insertOne(plantTraits);

        this.podsMapper.insertOne(pods);

        sppTraits.setPods(pods);
        this.sppTraitsMapper.insertOne(sppTraits);

        this.petioleMapper.insertOne(baclPetiole);

        this.leafletMapper.insertOne(baclLeaflet);

        baclTraits.setPetiole(baclPetiole);
        baclTraits.setLeaflet(baclLeaflet);
        this.baclTraitsMapper.insertOne(baclTraits);

        this.petioleMapper.insertOne(msclPetiole);

        this.leafletMapper.insertOne(msclLeaflet);

        msclTraits.setPetiole(msclPetiole);
        msclTraits.setLeaflet(msclLeaflet);
        this.msclTraitsMapper.insertOne(msclTraits);

        phenotype.setPlantTraits(plantTraits);
        phenotype.setBaclTraits(baclTraits);
        phenotype.setMsclTraits(msclTraits);
        phenotype.setSppTraits(sppTraits);
        this.phenotypeMapper.insertOne(phenotype);

        form.setPhenotype(phenotype);
        this.formMapper.insertOne(form);
    }

    @Override
    public Form findById(Integer formId) {
        return this.formMapper.selectById(formId);
    }

    @Override
    public boolean checkFormExistById(Integer formId) {
        return this.formMapper.selectFormNumById(formId) == 1;
    }

    @Override
    public List<Form> findFormTea(UserDto userDto) {
        return this.formMapper.selectFormTea(userDto);
    }

    @Override
    @Transactional
    public void modifyOneSimple(Form form) {
        this.formMapper.updateOne(form);
    }

    @Override
    @Transactional
    public void modifyOne(Form form, MainStem mainStem, FpolBranches fpolBranches, Branch branch,
                          Pods pods, Petiole baclPetiole, Leaflet baclLeaflet, Petiole msclPetiole,
                          Leaflet msclLeaflet) {
        if (PojoUtil.form.needUpdateForm(form))
            this.formMapper.updateOne(form);
        this.mainStemMapper.updateOne(mainStem);
        this.fpolBranchesMapper.updateOne(fpolBranches);
        this.branchMapper.updateOne(branch);
        this.podsMapper.updateOne(pods);
        this.petioleMapper.updateOne(baclPetiole);
        this.leafletMapper.updateOne(baclLeaflet);
        this.petioleMapper.updateOne(msclPetiole);
        this.leafletMapper.updateOne(msclLeaflet);
    }

}
